/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formula.one.urjc.circuito.view;

import formula.one.urjc.Metodos;
import formula.one.urjc.circuito.Circuito;
import formula.one.urjc.dueño.view.ventanaAviso;

/**
 *
 * @author santiago
 */
public class AltaCircuito extends javax.swing.JInternalFrame { 
    
    //VARIABLES GLOBALES
    //Declaramos este objeto para ejecutar los metodos genericos
    Metodos metodo = new Metodos() ;
    String nombreFichero = "circuitos.dat" ;    

    /**
     * Creates new form NewJInternalFrame
     */
    public AltaCircuito() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nombreCircuito = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        aforoCircuito = new javax.swing.JTextField();
        canonCircuito = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        aceptarAlta = new javax.swing.JButton();
        cancelarAlta = new javax.swing.JButton();
        jTextRectas = new javax.swing.JTextField();
        JTextCurvas = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        jLabel1.setText("Alta de Circuito");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Aforo:");

        jLabel4.setText("Canon:");

        jLabel5.setText("Rectas*:");

        jLabel6.setText("Curvas*:");

        aceptarAlta.setText("Aceptar");
        aceptarAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarAltaActionPerformed(evt);
            }
        });

        cancelarAlta.setText("Cancelar");
        cancelarAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarAltaActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Lucida Grande", 0, 12)); // NOI18N
        jLabel7.setText("*Valor de kilometros separado por ';'");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(121, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(139, 139, 139))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(aceptarAlta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addComponent(cancelarAlta)
                        .addGap(72, 72, 72))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nombreCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(canonCircuito, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                                        .addComponent(aforoCircuito, javax.swing.GroupLayout.Alignment.LEADING))
                                    .addComponent(jTextRectas, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(JTextCurvas, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(61, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nombreCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(aforoCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(canonCircuito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextRectas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(JTextCurvas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aceptarAlta)
                    .addComponent(cancelarAlta))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarAltaActionPerformed
        // TODO add your handling code here:
        this.dispose() ;
    }//GEN-LAST:event_cancelarAltaActionPerformed

    private void aceptarAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarAltaActionPerformed
        // TODO add your handling code here:
        // Obtenemos los valores del formulario
        String nombre_Circuito = nombreCircuito.getText() ;
        int aforo_Circuito = Integer.parseInt(aforoCircuito.getText()) ;
        int canon_Circuito = Integer.parseInt(canonCircuito.getText()) ;
        String rectas_Circuito = jTextRectas.getText();
        String curvas_Circuito = JTextCurvas.getText();
        //
        //comprobamos cuantos circuitos hay dados de alta en el archivo
        if(metodo.numeroCircuitos()<5){
            // Declaramos el objeto de nuevo circuito
            Circuito nuevo_Circuito = new Circuito(nombre_Circuito, aforo_Circuito,
                                               canon_Circuito, rectas_Circuito,
                                               curvas_Circuito) ;
            //compruebo que existan curvas o rectas
            if(nuevo_Circuito.getCurvas()!=null && nuevo_Circuito.getRectas()!=null){
                System.out.println(nuevo_Circuito.toString()) ;
                // Guardamos el Objeto en el fichero
                metodo.escribirFichero(nombreFichero, nuevo_Circuito) ; 
            }else{
                ventanaAviso.mostrarError(this, "No se ha prodido crear el circuito.\nError al introducir los km de las curvas o rectas", "Error alta circuito");
            }
        }else{
            ventanaAviso.mostrarError(this, "No se pueden dar de alta más de cinco circuitos", "Error alta circuito");
        }
        
        
    }//GEN-LAST:event_aceptarAltaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTextCurvas;
    private javax.swing.JButton aceptarAlta;
    private javax.swing.JTextField aforoCircuito;
    private javax.swing.JButton cancelarAlta;
    private javax.swing.JTextField canonCircuito;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextRectas;
    private javax.swing.JTextField nombreCircuito;
    // End of variables declaration//GEN-END:variables
}
